package cn.wolfcode.service.impl;

import cn.wolfcode.common.domain.UserInfo;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.mapper.OrderInfoMapper;
import cn.wolfcode.mapper.PayLogMapper;
import cn.wolfcode.mapper.RefundLogMapper;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.service.ISeckillProductService;
import cn.wolfcode.util.IdGenerateUtil;
import cn.wolfcode.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by wolfcode-lanxw
 */
@Service
public class OrderInfoSeviceImpl implements IOrderInfoService {
    @Autowired
    private ISeckillProductService seckillProductService;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private PayLogMapper payLogMapper;
    @Autowired
    private RefundLogMapper refundLogMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String doSeckill(SeckillProductVo seckillProductVo, UserInfo user) {
        //库存减一
        seckillProductService.decrStockCount(seckillProductVo);
        //生成订单
        String oder = this.creatOrder(seckillProductVo,user);
        //生成redis表示防止重复提交
//        String realKey = SeckillRedisKey.SECKILL_ORDER_SET.getRealKey(seckillProductVo.getTime() + "");
//        redisTemplate.opsForSet().add(realKey,user.getPhone()+":"+seckillProductVo.getId());
        return oder;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String doSeckill(Integer time, Long seckillId, String token) {
        SeckillProductVo seckillProductVo = seckillProductService.find(time, seckillId);
        UserInfo user = UserUtil.getUser(redisTemplate, token);
        return doSeckill(seckillProductVo,user);
    }

    private String creatOrder(SeckillProductVo seckillProductVo, UserInfo user) {
        OrderInfo order = new OrderInfo();
        Date now = new Date();
        order.setCreateDate(now);
        String orderNo = IdGenerateUtil.get().nextId()+"";
        order.setOrderNo(orderNo);
        order.setProductCount(1);
        order.setProductId(seckillProductVo.getProductId());
        order.setProductImg(seckillProductVo.getProductImg());
        order.setProductName(seckillProductVo.getProductName());
        order.setProductPrice(seckillProductVo.getProductPrice());
        order.setSeckillDate(seckillProductVo.getStartDate());
        order.setSeckillTime(seckillProductVo.getTime());
        order.setSeckillId(seckillProductVo.getId());
        order.setSeckillPrice(seckillProductVo.getSeckillPrice());
        order.setUserId(user.getPhone());
        order.setStatus(OrderInfo.STATUS_ARREARAGE);
        orderInfoMapper.insert(order);
        return orderNo;
    }
}
